package com.hup.sidebar.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.hup.sidebar.R;
import com.hup.sidebar.SideBarApplication;
import com.hup.sidebar.model.AppConfig;
import com.hup.sidebar.view.component.UndoManager;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 编辑便签
 *
 * @author hugan
 * @date 2022/6/11
 */
@SuppressLint("NonConstantResourceId")
public class NoteActivity extends AppCompatActivity {
    private static final String TAG = NoteActivity.class.getSimpleName();

    @BindView(R.id.txtTips)
    TextView txtTips;
    @BindView(R.id.txtNote)
    EditText txtNote;

    private AppConfig appConfig;
    private UndoManager undoManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
        appConfig = SideBarApplication.getBean(AppConfig.class);

        setContentView(R.layout.note_activity);
        ButterKnife.bind(this);
        txtNote.setText(appConfig.noteData);

        //listener
        txtNote.setFilters(new InputFilter[]{new InputFilter.LengthFilter(AppConfig.NOTE_MAX_LENGTH)});
        undoManager = UndoManager.setUndoManager(txtNote);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                txtTips.setText(getString(R.string.note_save_tips, length + "/" + AppConfig.NOTE_MAX_LENGTH));
            }
        };
        txtNote.addTextChangedListener(textWatcher);
        textWatcher.afterTextChanged(txtNote.getText());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.common_editor_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_undo) {
            undoManager.undo();
        } else if (item.getItemId() == R.id.menu_redo) {
            undoManager.redo();
        } else if (item.getItemId() == R.id.menu_save) {
            Log.d(TAG, "onOptionsItemSelected: menu_save");
            saveNote(true);
        }
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (!isFinishing()) {
            Log.i(TAG, "onPause: ");
            saveNote(true);
        }
    }

    @Override
    public void finish() {
        super.finish();
        Log.i(TAG, "finish: ");
        saveNote(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    private void saveNote(boolean setText) {
        appConfig.setNoteData(txtNote.getText().toString().trim());
        appConfig.saveNoteData(this);

        if (setText) {
            txtNote.setText(appConfig.noteData);
        }
    }

}
